package net.aiform.entity.security;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springside.modules.utils.CollectionUtils;

import net.aiform.entity.IdEntity;
import net.aiform.entity.org.Sysuser;

/**
 * Sysrole entity.
 * 
 * @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "sysrole")
public class Sysrole  extends IdEntity {

	// Fields

	private String roleName;
	private String remark;
	private Set<Authority> authorities = new HashSet<Authority>(0);
	private Set<Sysuser> sysusers = new HashSet<Sysuser>(0);

	@Column(name = "ROLE_NAME", length = 60)
	public String getRoleName() {
		return this.roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	@Column(name = "REMARK", length = 256)
	public String getRemark() {
		return this.remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
	@JoinTable(name = "role_authority", catalog = "framework", joinColumns = { @JoinColumn(name = "SYS_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "ID", nullable = false, updatable = false) })
	public Set<Authority> getAuthorities() {
		return this.authorities;
	}

	public void setAuthorities(Set<Authority> authorities) {
		this.authorities = authorities;
	}

	@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY, mappedBy = "sysroles")
	public Set<Sysuser> getSysusers() {
		return this.sysusers;
	}

	public void setSysusers(Set<Sysuser> sysusers) {
		this.sysusers = sysusers;
	}

	@Transient
	public String getAuthNames() throws Exception {
		return CollectionUtils.fetchPropertyToString(authorities, "authName", ", ");
	}

	@SuppressWarnings("unchecked")
	@Transient
	public List<String> getAuthIds() throws Exception {
		return CollectionUtils.fetchPropertyToList(authorities, "id");
	}
}